//
//  LBMStatusRetweetedView.swift
//  微博
//
//  Created by 刘保满 on 16/5/25.
//  Copyright © 2016年 itheima. All rights reserved.
//  转发微博视图

import UIKit
import SnapKit

class LBMStatusRetweetedView: UIView {
    
    //MARK:转发微博视图中的配图图片也会出现不存在的时候,所以也要进行判断赋值,并修改这个视图的底部约束
    var bottomCons: Constraint?
    
    
    //绑定视图模型
    var statusViewModel: LBMStatusViewModel? {
        
        didSet {
            //设置数据
            contentLabel.text = statusViewModel?.status?.retweeted_status?.text
            
            //            pictureView.pictures = statusViewModel?.status?.retweeted_status?.pic_urls
            
            //在这里进行判断赋值,修改约束
            self.bottomCons?.uninstall()
            if let urls = statusViewModel?.status?.retweeted_status?.pic_urls where urls.count > 0 {
                //说明有配图
                //设置数据
                pictureView.pictures = urls
                //显示配图
                pictureView.hidden = false
                //更新转发微博视图的底部约束
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                
            } else {
                //没有配图
                //设置数据
                pictureView.pictures = nil
                //隐藏
                pictureView.hidden = true
                //更新转发微博视图的底部约束
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    }
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
        
    }
    
    //默认报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    private func setupUI() {
        
        //设置视图背景颜色
       backgroundColor = UIColor(red: 242/255.0, green: 242/255.0, blue: 245/255.0, alpha: 1.0)
        //添加子控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置约束
        contentLabel.snp_makeConstraints { (make) in
            
            make.left.top.equalTo(self).offset(StatusCellMargin)
            
        }
        //配图
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
            
        }
        //给这个view本身添加底部约束
        self.snp_makeConstraints { (make) in
            //记录当前的约束
            self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
    
    //懒加载子控件
    private lazy var contentLabel: UILabel = UILabel(title: "那年青春我们正好那年青春我们正好那年青春我们正好那年青春我们正好那年青春我们正好那年青春我们正好那年青春我们正好那年青春我们正好", font: 15, textColor: UIColor.darkGrayColor(), alignmet: .Left)
    
    //配图
    private lazy var pictureView: LBMStatusPictureView = LBMStatusPictureView()
    
}
